"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 硬件故障类--磁盘
Case Name   : 备机重建时主机磁盘满
Create At   : 2021/07/13
@zou_jialiang0505328126
Description :
    1.主节点注入磁盘满故障
    2.备节点重建
    3.清除故障
    4.重启主节点
    5.备节点重建
Expect      :
    1.成功
    2.重建失败
    3.成功
    4.成功
    5.成功
History     :
"""

import unittest
from yat.test import Node, macro
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.CommonSH import RestartDbCluster
from testcase.utils.Logger import Logger



class CPUOverloadAndRunTPCC(unittest.TestCase):
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info(
            '---------Opengauss_Reliability_Hardware_Case043:开始执行---------')
        self.primary_root_node = Node(node='PrimaryRoot')
        self.com = Common()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.sta1_sh = CommonSH('Standby1DbUser')
        self.constant = Constant()

        self.log.info("-----------获取磁盘名 -----------")
        cmd = f"df -h {macro.DB_INSTANCE_PATH}"
        self.log.info(cmd)
        msg = self.primary_root_node.sh(cmd).result()
        self.log.info(msg)
        self.disk_name = msg.splitlines()[-1].split()[0].strip()

    def test_main(self):
        self.log.info("--------------1.主节点注入磁盘满故障-------------")
        inject_command = f"rfile_full (diskname) values ({self.disk_name})"
        result = self.com.cfe_inject(self.primary_root_node, inject_command)
        self.log.info(result)
        self.assertIn("successful execution", result)

        self.log.info("----------------2.备节点重建-------------")
        result = self.sta1_sh.executeGsctl(
            "build", self.constant.REBUILD_SUCCESS_MSG)
        self.assertFalse(result)

        self.log.info("--------------3.主节点清理磁盘满故障-------------")
        clean_command = f"rfile_full  where(diskname={self.disk_name})"
        result = self.com.cfe_clean(self.primary_root_node, clean_command)
        self.log.info(result)
        self.assertIn("successful execution", result)

        self.log.info("----------------4.重启主节点-------------------------")
        result = self.pri_sh.startDbInstance()
        self.assertIn(self.constant.RESTART_SUCCESS_MSG, result)

        self.log.info("----------------5.备节点重建-------------")
        result = self.sta1_sh.executeGsctl(
            "build", self.constant.REBUILD_SUCCESS_MSG)
        self.assertTrue(result)

    def tearDown(self):
        self.log.info("--------------主节点清理磁盘满故障-------------")
        clean_command = f"rfile_full  where(diskname={self.disk_name})"
        result = self.com.cfe_clean(self.primary_root_node, clean_command)
        self.log.info(result)
        self.log.info("------------重启主节点--------------")
        self.pri_sh.startDbInstance()
        self.log.info('-----Opengauss_Reliability_Hardware_Case043:执行完成-----')
